#! /usr/bin/env bash

# 仅编译FFmpeg库

set -e

function getAbiName() {
  case $1 in
  armv5)
    echo "armeabi"
    ;;

  armv7a)
    echo "armeabi-v7a"
    ;;

  arm64)
    echo "arm64-v8a"
    ;;

  x86)
    echo "x86"
    ;;

  x86_64)
    echo "x86_64"
    ;;

  esac
}

# 当前执行脚本所在的文件夹
SCRIPT_DIR=$(
  cd "$(dirname "$0")"
  pwd
)

cd ${SCRIPT_DIR}

TARGET_LIBS_PATH=../libs/jniLibs

# 配置环境路径
. ./local-environment-config.sh

OS=$1

# 源码路径
SOURCE_REPO_DIR=${BUILD_DIR}/${OS}/repos

# 清理之前的Build文件
sh ./clean-build.sh ${BUILD_DIR}
sh ./clean-build.sh ${TARGET_LIBS_PATH}

for target in "$@"; do
  case ${target} in

  armv5 | armv7a | arm64 | x86 | x86_64)
    # 初始化
    sh ./init-ffmpeg.sh ${OS} ${target} ${CODE_REPOS_DIR} ${BUILD_DIR}
    # 编译
    sh ./${OS}/build-ffmpeg.sh ${OS} ${target} ${SOURCE_REPO_DIR}/FFmpeg-${target}
    # 收集产物
    abi=$(getAbiName ${target})
    dest_path=${TARGET_LIBS_PATH}/${abi}
    mkdir -p ${dest_path}
    mkdir -p ${dest_path}/ffmpeg
    cp ${BUILD_DIR}/${OS}/ffmpeg-${target}/shared/lib/libffmpeg.so ${dest_path}
    cp -r ${BUILD_DIR}/${OS}/ffmpeg-${target}/include ${dest_path}/ffmpeg
    ;;

  *) ;;

  esac
done
